package com.gkwave.web.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.action.service.BaseService;
import com.gkwave.module.bean.User;
import com.gkwave.module.dao.UserDAO;

/**
 * @author Dhanasekar Rengasamy
 * @version 1.0.0
 * Servlet implementation class LoginServlet
 */
public class UserCreateAction extends BaseService {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserCreateAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Proceses requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void procesRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String username = request.getParameter("_user_name");
    	String password = request.getParameter("_password");
    	String fullname = request.getParameter("_user_full_name");
    	String idEmail = request.getParameter("_user_id_email");
    	String message = null;
    	
    	System.out.println("_user_name: "+ username);
    	System.out.println("_password: "+ password);
    	System.out.println("_user_full_name: "+ fullname);
    	System.out.println("_user_id_email: "+ idEmail);
    	
    	String nextJSP = "/WEB-INF/jsp/UserCreationPage.jsp";
    	UserDAO dao = new UserDAO();    	
    	if(username!=null && password!=null && fullname!=null && idEmail!=null){
        	if(username.trim().length()>0 && password.trim().length()>0 && fullname.trim().length()>0 && idEmail.trim().length()>0){
        		boolean status = dao.createUser(username,password,fullname,idEmail);
        		if(status){
        			message = "Data saved successfully";
        		}else{
        			message = "Error!!!";
        		}
        	} else{
        		message = "Empty Input!!!";
        	}
    	}
    	
    	List<User> list = dao.getUsers();
    	
    	request.setAttribute("user", list);
    	request.setAttribute("message", message);
    	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    	dispatcher.forward(request,response);
    }

}
